package com.wzk.controller;

import com.wzk.annotation.LogFlag;
import com.wzk.pojo.Dept;
import com.wzk.pojo.Result;
import com.wzk.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result getDepts() {
        log.info("获取数据列表...");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("获取id为{}的数据",id);
        return Result.success(deptService.getById(id));
    }
    @LogFlag
    @DeleteMapping
    public Result deleteDept(Integer id) {
        log.info("删除id为{}的数据",id);
        //看看这个部门还有员工吗
        boolean flag = deptService.getCount(id);
        if (flag) {
            return Result.error("对不起，当前部门下有员工，不能直接删除！");
        }else{
        deptService.delete(id);
        return Result.success();
        }
    }
    @LogFlag
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("新增数据"+dept);
        deptService.addDept(dept);
        return Result.success();
    }
    @LogFlag
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("更新数据"+dept);
        deptService.update(dept);
        return Result.success();
    }
}
